package com.cuitrip.business.tripservice;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.cuitrip.business.tripservice.model.OtherNotes;
import com.cuitrip.component.para.model.Para;
import com.cuitrip.component.para.proxy.ParaProxy;
import com.cuitrip.business.tripservice.ui.EditDetailLayout;
import com.cuitrip.service.R;
import com.cuitrip.util.track.TrackUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.jumper.Jumper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Bind;


/**
 * 行程其他说明
 * Created ak
 */
public class TripOtherInstructionActivity extends CustomUiFragmentActivity {

    @Bind(R.id.trip_attention)
    protected EditDetailLayout mTripAttention;

    @Bind(R.id.trip_safe_tips)
    protected EditDetailLayout mTripSafeTips;

    @Bind(R.id.trip_clothes_tips)
    protected EditDetailLayout mTripClothTips;

    @Bind(R.id.trip_weather_tips)
    protected EditDetailLayout mTripWeatherTips;

    public final static int ATTENTION_TYPE = 0x01;
    public final static int SAFE_TIPS_TYPE = 0x02;
    public final static int CLOTH_TIPS_TYPE = 0x03;
    public final static int WEATHER_TIPS_TYPE = 0x04;

    private final static int TEXT_MAX_LIMIT = 1500;

    public static final String OTHER_NOTE_KEY = "othernotes";

    public static final String ATTENTION_KEY = "attention_key";
    public static final String SAFE_KEY = "safe_key";
    public static final String CLOTH_KEY = "cloth_key";
    public static final String WEATHER_KEY = "weather_key";

    private HashMap<Integer, ArrayList<CharSequence>> paraListArray = new HashMap<>();
    private OtherNotes mOtherNotes;

    public static final String EDIT_KEY = "edit";

    private boolean isEdit = false;

    public static Intent getOtherNotesIntent(Activity context, OtherNotes otherNotes, boolean isEdit){
        Intent intent = new Intent(context, TripOtherInstructionActivity.class);
        intent.putExtra(EDIT_KEY, isEdit);
        intent.putExtra(OTHER_NOTE_KEY, otherNotes);
        return intent;
    }

    public static void startOtherInstructionLayout(Context context, OtherNotes otherNotes, boolean isEdit){
        Intent intent = new Intent(context, TripOtherInstructionActivity.class);
        intent.putExtra(EDIT_KEY, isEdit);
        intent.putExtra(OTHER_NOTE_KEY, otherNotes);
        Jumper.launchActivity(context, intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (getIntent().hasExtra(OTHER_NOTE_KEY)) {
            mOtherNotes = (OtherNotes) getIntent().getSerializableExtra(OTHER_NOTE_KEY);
        }
        if (getIntent().hasExtra(EDIT_KEY)){
            isEdit = getIntent().getBooleanExtra(EDIT_KEY, false);
        }

        super.onCreate(savedInstanceState,R.layout.page_other_instruction);
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        if (isEdit){
            initNavigation();
            mTripAttention.showRightHint(true);
            mTripSafeTips.showRightHint(true);
            mTripClothTips.showRightHint(true);
            mTripWeatherTips.showRightHint(true);
        }else{
            mTripAttention.showRightHint(false);
            mTripSafeTips.showRightHint(false);
            mTripClothTips.showRightHint(false);
            mTripWeatherTips.showRightHint(false);
        }
        initTitle();

    }

    @Override
    public void initData() {
        initValue();
        initValue(ATTENTION_TYPE, SAFE_TIPS_TYPE, CLOTH_TIPS_TYPE, WEATHER_TIPS_TYPE);
    }

    private void initTitle() {
        mTripAttention.setTitle(getString(R.string.trip_attribute_advice));
        mTripClothTips.setTitle(getString(R.string.trip_attribute_outfit));
        mTripWeatherTips.setTitle(getString(R.string.trip_attribute_weather));
        mTripSafeTips.setTitle(getString(R.string.trip_attribute_safety));
    }

    private void initValue(int... contentTypes) {
        for (int contentType : contentTypes){
            initTypeValue(contentType);
        }
    }

    private void initTypeValue(int contentType){
        switch (contentType){
            case SAFE_TIPS_TYPE:
                if (TextUtils.isEmpty(mOtherNotes.getServiceSafeTips()) && !isEdit){
                    mTripSafeTips.setVisibility(View.GONE);
                }else{
                    ArrayList<CharSequence> stringArray = ParaProxy.getInstance().getParaList(mOtherNotes.getServiceSafeTips());
                    setValueInContent(SAFE_TIPS_TYPE, stringArray);
                }
                break;
            case ATTENTION_TYPE:
                if (TextUtils.isEmpty(mOtherNotes.getServiceNotice()) && !isEdit){
                    mTripAttention.setVisibility(View.GONE);
                }else{
                    ArrayList<CharSequence> stringArray = ParaProxy.getInstance().getParaList(mOtherNotes.getServiceNotice());
                    setValueInContent(ATTENTION_TYPE, stringArray);
                }
                break;
            case CLOTH_TIPS_TYPE:
                if (TextUtils.isEmpty(mOtherNotes.getServiceWearTips()) && !isEdit){
                    mTripClothTips.setVisibility(View.GONE);
                }else{
                    ArrayList<CharSequence> stringArray = ParaProxy.getInstance().getParaList(mOtherNotes.getServiceWearTips());
                    setValueInContent(CLOTH_TIPS_TYPE, stringArray);
                }
                break;
            case WEATHER_TIPS_TYPE:
                if (TextUtils.isEmpty(mOtherNotes.getUnexpectedWeather()) && !isEdit){
                    mTripWeatherTips.setVisibility(View.GONE);
                }else{
                    ArrayList<CharSequence> stringArray = ParaProxy.getInstance().getParaList(mOtherNotes.getUnexpectedWeather());
                    setValueInContent(WEATHER_TIPS_TYPE, stringArray);
                }
                break;
        }
    }

    private void initNavigation() {
        mTripAttention.setNavigateAction(new EditDetailLayout.NavigateAction() {
            @Override
            public void navigate() {
                Bundle bundle = new Bundle();
                bundle.putString(ParaEditActivity.TITLE_KEY, getString(R.string.trip_attribute_advice));
                if (paraListArray.get(ATTENTION_TYPE) != null){
                    bundle.putCharSequenceArrayList(ParaEditActivity.PARA_KEY, paraListArray.get(ATTENTION_TYPE));
                }
                if (TripEditActivity.ContentMode.NEW_MODE.ordinal() == mOtherNotes.getContentMode().ordinal()){
                    TrackUtil.trackScreenName("/insider/trips/create/set/additional/notices");
                }else {
                    TrackUtil.trackScreenName("/insider/trips/edit/set/additional/notices");
                }

                bundle.putInt(ParaEditActivity.MAX_LIMIT_KEY, TEXT_MAX_LIMIT);
                ParaEditActivity.launchParaEditActivity(TripOtherInstructionActivity.this, bundle, ATTENTION_TYPE);
            }
        });

        mTripSafeTips.setNavigateAction(new EditDetailLayout.NavigateAction() {
            @Override
            public void navigate() {
                if (TripEditActivity.ContentMode.NEW_MODE.ordinal() == mOtherNotes.getContentMode().ordinal()){
                    TrackUtil.trackScreenName("/insider/trips/create/set/additional/safety");
                }else {
                    TrackUtil.trackScreenName("/insider/trips/edit/set/additional/safety");
                }
                Bundle bundle = new Bundle();
                bundle.putString(ParaEditActivity.TITLE_KEY, getString(R.string.trip_attribute_safety));
                if (paraListArray.get(SAFE_TIPS_TYPE) != null){
                    bundle.putCharSequenceArrayList(ParaEditActivity.PARA_KEY, paraListArray.get(SAFE_TIPS_TYPE));
                }
                bundle.putInt(ParaEditActivity.MAX_LIMIT_KEY, TEXT_MAX_LIMIT);
                ParaEditActivity.launchParaEditActivity(TripOtherInstructionActivity.this, bundle, SAFE_TIPS_TYPE);
            }
        });

        mTripClothTips.setNavigateAction(new EditDetailLayout.NavigateAction() {
            @Override
            public void navigate() {
                if (TripEditActivity.ContentMode.NEW_MODE.ordinal() == mOtherNotes.getContentMode().ordinal()){
                    TrackUtil.trackScreenName("/insider/trips/create/set/additional/outfit");
                }else {
                    TrackUtil.trackScreenName("/insider/trips/edit/set/additional/outfit");
                }
                Bundle bundle = new Bundle();
                bundle.putString(ParaEditActivity.TITLE_KEY, getString(R.string.trip_attribute_outfit));
                if (paraListArray.get(CLOTH_TIPS_TYPE) != null){
                    bundle.putCharSequenceArrayList(ParaEditActivity.PARA_KEY, paraListArray.get(CLOTH_TIPS_TYPE));
                }
                bundle.putInt(ParaEditActivity.MAX_LIMIT_KEY, TEXT_MAX_LIMIT);
                ParaEditActivity.launchParaEditActivity(TripOtherInstructionActivity.this, bundle, CLOTH_TIPS_TYPE);
            }
        });

        mTripWeatherTips.setNavigateAction(new EditDetailLayout.NavigateAction() {
            @Override
            public void navigate() {
                if (TripEditActivity.ContentMode.NEW_MODE.ordinal() == mOtherNotes.getContentMode().ordinal()){
                    TrackUtil.trackScreenName("/insider/trips/create/set/additional/weather");
                }else {
                    TrackUtil.trackScreenName("/insider/trips/edit/set/additional/weather");
                }
                Bundle bundle = new Bundle();
                bundle.putString(ParaEditActivity.TITLE_KEY, getString(R.string.trip_attribute_weather));
                if (paraListArray.get(WEATHER_TIPS_TYPE) != null){
                    bundle.putCharSequenceArrayList(ParaEditActivity.PARA_KEY, paraListArray.get(WEATHER_TIPS_TYPE));
                }
                bundle.putInt(ParaEditActivity.MAX_LIMIT_KEY, TEXT_MAX_LIMIT);
                ParaEditActivity.launchParaEditActivity(TripOtherInstructionActivity.this, bundle, WEATHER_TIPS_TYPE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            ArrayList<String> highList = data.getStringArrayListExtra(ParaEditActivity.PARA_KEY);
            setValueInContent(requestCode, highList);
        }
    }

    private void setValueInContent(int contentType, Object value) {
        switch (contentType) {
            case ATTENTION_TYPE:

                setValueToLayout(mTripAttention, contentType, value);
                break;
            case CLOTH_TIPS_TYPE:

                setValueToLayout(mTripClothTips, contentType, value);
                break;
            case WEATHER_TIPS_TYPE:

                setValueToLayout(mTripWeatherTips, contentType, value);
                break;
            case SAFE_TIPS_TYPE:

                setValueToLayout(mTripSafeTips, contentType, value);
                break;
        }
    }

    private void setValueToLayout(EditDetailLayout editDetailLayout, int type, Object value) {
        List<Para> paraList = new ArrayList<>();
        if (value instanceof List) {
            ArrayList<CharSequence> valueList = (ArrayList<CharSequence>) value;
            for (CharSequence charSequence : valueList) {
                Para para = new Para(charSequence);
                paraList.add(para);
            }
            paraListArray.put(type, valueList);
        } else {
            paraListArray.remove(type);
        }
        editDetailLayout.setParaList(paraList);
    }

    @Override
    public void onBackPressed() {
        saveAction();
    }

    private void saveAction(){
        Intent intent = new Intent();
        intent.putCharSequenceArrayListExtra(ATTENTION_KEY, paraListArray.get(ATTENTION_TYPE));
        intent.putCharSequenceArrayListExtra(SAFE_KEY, paraListArray.get(SAFE_TIPS_TYPE));
        intent.putCharSequenceArrayListExtra(CLOTH_KEY, paraListArray.get(CLOTH_TIPS_TYPE));
        intent.putCharSequenceArrayListExtra(WEATHER_KEY, paraListArray.get(WEATHER_TIPS_TYPE));
        setResult(RESULT_OK, intent);
        finish();
    }

    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config=new CustomUiConfig();
        config.mTopbarLeftAreaName=getString(R.string.back_icon);
        config.mTopbarMiddleAreaName=getString(R.string.trip_group_additional_info);
        return config;
    }

    @Override
    public void onTopbarLeftPress() {
        saveAction();
        onBackPressed();
    }
}
